package com.wanxi.config.handler;

import com.alibaba.fastjson.JSON;
import com.wanxi.dto.ResultDto;
import com.wanxi.util.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Li
 * @Date 2023/7/15 10:43
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
//     授权失败
        ResultDto resultDto = new ResultDto(HttpStatus.FORBIDDEN.value(), "该用户没有此权限");
//        转换成JSON的字符串
        String jsonString = JSON.toJSONString(resultDto);
        WebUtils.renderString(response, jsonString);
    }

}
